<?xml version="1.0" encoding="UTF-8"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<jsp:text><![CDATA[
<script type="text/javascript">
    function logout() {
        document.getElementById("loginInfo").todo.value = "logout";
        document.getElementById("loginInfo").submit();
    }

    function showProfile() {
        document.getElementById("loginInfo").todo.value = "showProfile";
        document.getElementById("loginInfo").submit();
    }

    function showManagementPage() {
        document.getElementById("loginInfo").todo.value = "showManagementPage";
        document.getElementById("loginInfo").submit();
    }

    function validate()
    {
        var validationResult=true;
        var inputEmail=document.getElementById("login").email.value;
        var atpos=inputEmail.indexOf("@");
        var dotpos=inputEmail.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=inputEmail.length){
            document.getElementById('emailValidation').innerText = "Not a valid email.";
            validationResult=false;
        }
        else {
            document.getElementById('emailValidation').innerText = "";
        }

        var inputPwd=document.getElementById("login").password.value;
        if (inputPwd==null || inputPwd==""){
            document.getElementById('pwdValidation').innerText = "Please input your password";
            validationResult=false;
        }
        else {
             document.getElementById('pwdValidation').innerText = "";
        }
        return validationResult;
    }
</script>
]]></jsp:text>


<jsp:scriptlet>
final String MANAGER = "manager";
final String OFFICER = "officer";
final String CUSTOMER = "customer";

if (session.getAttribute("loginResult") != null &amp;&amp;(Boolean)session.getAttribute("loginResult")) {</jsp:scriptlet>
    <jsp:text><![CDATA[Welcome! ]]></jsp:text><jsp:expression>session.getAttribute("email")</jsp:expression> <jsp:text><![CDATA[<br />
    Your role: ]]></jsp:text><jsp:expression>session.getAttribute("role")</jsp:expression> <jsp:text><![CDATA[<br />
    <form name="loginInfo" id="loginInfo" method="post" action=" ]]></jsp:text><jsp:expression>request.getContextPath()</jsp:expression><jsp:text><![CDATA[/index.do">
        <input type="hidden" name="todo" id="todo" value="" />]]></jsp:text>
    <jsp:scriptlet>
    if (((String)session.getAttribute("role")).equalsIgnoreCase(CUSTOMER)) {</jsp:scriptlet>
        <jsp:text><![CDATA[<input type='button' onclick='showProfile();' value='My Profle' /> ]]></jsp:text>
    <jsp:scriptlet>}
    else{</jsp:scriptlet>
        <jsp:text><![CDATA[ <input type='button' onclick='showManagementPage();' value='Management Page' /> ]]></jsp:text>
    <jsp:scriptlet>}</jsp:scriptlet>
    <jsp:text><![CDATA[
    <input type='button' onclick='logout();' value='logout' />
    </form>]]></jsp:text>
<jsp:scriptlet>}
else {</jsp:scriptlet>
    <jsp:text><![CDATA[
     <h2>Login</h2>
        <form name="login" id="login" onsubmit="return validate();" method="post" action="]]></jsp:text><jsp:expression>(String)request.getAttribute("javax.servlet.forward.request_uri")</jsp:expression><jsp:text><![CDATA[">
            <div>
                <div id="label"><b>Email Address :</b></div>
                <div class="roundedfield" >
                    <input type="text" id="email" name="email" size="20" maxlength="20"/> <span style="color:red" id="emailValidation"></span>
                </div>
            </div>
            <div>
                <div id="label"><b>Password :</b></div>
                <div class="roundedfield" >
                    <input type="password" id="password" name="password"  size="20" maxlength="20"/> <span style="color:red" id="pwdValidation"></span>
                </div>
            </div><br />]]></jsp:text>

            <jsp:scriptlet>
                if (session.getAttribute("loginResult") != null) { // loginResult == false</jsp:scriptlet>
                    <jsp:text><![CDATA[<p>]]></jsp:text><jsp:expression>session.getAttribute("msg")</jsp:expression><jsp:text><![CDATA[</p>]]></jsp:text>
                    <jsp:scriptlet>session.removeAttribute("loginResult");
                }
            </jsp:scriptlet>
            <jsp:text><![CDATA[
            <input type="hidden" name="todo" id="todo" value="login" />
            <input type='submit' value='Login' />
         </form>]]></jsp:text>
<jsp:scriptlet>}</jsp:scriptlet>
</jsp:root>
